﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.OleDb;

namespace DAO
{
    public class LopDAO : AbstractDAO<LopDTO>
    {

        protected override string GetQueryLayDanhSach()
        {
            return "select * from Lop";
        }

        public static LopDTO LayPhongTuDataRow(DataRow dr)
        {
            LopDTO item = null;
            try
            {
                item = new LopDTO();
                item.ID = dr["ID"].ToString();
                item.IDKhoi = dr["IDKhoi"].ToString();
                item.TenLop = dr["TenLop"].ToString();
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi lấy thông tin: " + ex.ToString());
            }
            return item;
        }

        //lấy danh sách đầy đủ
        public static List<LopDTO> LayDanhSach()
        {
            List<LopDTO> lstLopDTO = null;
            try
            {
                lstLopDTO = new List<LopDTO>();
                String query = "SELECT * From Lop";
                DataTable dt = DataProvider.ExecuteQuery(query);
                foreach (DataRow dr in dt.Rows)
                {
                    LopDTO item = new LopDTO();
                    item = LayPhongTuDataRow(dr);
                    lstLopDTO.Add(item);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi khi lấy DS: " + ex.ToString());
            }
            return lstLopDTO;
        }

        protected override string GetInsertQuery()
        {
            return "Insert into Lop values (?,?,?)";
        }

        protected override string GetUpdateQuery()
        {
            return "Update Lop Set TenLop = ?, IDKhoi = ? Where ID = ?";
        }

        protected override string GetDeleteQuery()
        {
            return "Delete From Lop Where ID = ?";
        }

        protected override LopDTO GetObjectFromDataRow(DataRow dr)
        {
            LopDTO item = new LopDTO();
            item.ID = dr["ID"].ToString();
            item.IDKhoi = dr["IDKhoi"].ToString();
            item.TenLop = dr["TenLop"].ToString();
            return item;
        }

        protected override IList<OleDbParameter> GetListParameter(LopDTO item)
        {
            IList<OleDbParameter> arrParameter = new List<OleDbParameter>();
            arrParameter.Add(new OleDbParameter("@ID", item.ID));
            arrParameter.Add(new OleDbParameter("@IDKhoi", item.IDKhoi));
            arrParameter.Add(new OleDbParameter("@TenLop", item.TenLop));
            return arrParameter;
        }

        protected override string GetQueryGetByID(string id)
        {
            throw new NotImplementedException();
        }
    }
}
